package com.shicijiaoliu.service.impl;

import com.github.pagehelper.PageHelper;
import com.shicijiaoliu.service.Authorservice;
import com.shicijiaoliu.mapper.Authormapper;
import com.shicijiaoliu.pojo.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthorserviceImpl implements Authorservice {

    @Autowired
    private Authormapper authormapper;
    @Override
    public void addauthor(Author author) {
        authormapper.add(author);
    }

    @Override
    public Author findByname(String name) {
        Author author = authormapper.findByname(name);
        return author;
    }

    @Override
    public List<Author> findauthorBypage(int pagecount, int pagenumber,String name, String dynasty) {
        int count = authormapper.getcount(null,null);
        int totalpage = (int) Math.ceil((double) count/pagenumber);
        if(pagecount>totalpage || pagecount <= 0){
            return null;
        }else{
            PageHelper.startPage(pagecount,pagenumber);
            return authormapper.findauthorBypage(name,dynasty);
        }
    }

    @Override
    public int getcount(String name,String dynasty) {
        return authormapper.getcount(name,dynasty);
    }

    @Override
    public void updateauthor(Author author) {
        authormapper.updateauthor(author);
    }

    @Override
    public Author findByid(long id) {
        return authormapper.findByid(id);
    }

    @Override
    public void delauthor(long id) {
        authormapper.delauthor(id);
    }
}
